home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / c / BinaryTrees.lzh / ubi_SplayTree.h < prev   
Encoding:
C/C++ Source or Header  |  1997-07-26  |  15.3 KB  |  331 lines

  1. #ifndef ubi_SplayTree_H
  2. #define ubi_SplayTree_H
  3. /* ========================================================================== **
  4.  *                              ubi_SplayTree.h
  5.  *
  6.  *  Copyright (C) 1993,1995 by Christopher R. Hertel
  7.  *
  8.  *  Email: crh@ubiqx.mn.org
  9.  * -------------------------------------------------------------------------- **
  10.  *
  11.  *  This module implements "splay" trees.  Splay trees are binary trees
  12.  *  that are rearranged (splayed) whenever a node is accessed.  The
  13.  *  splaying process *tends* to make the tree bushier (improves balance),
  14.  *  and the nodes that are accessed most frequently *tend* to be closer to
  15.  *  the top.
  16.  *
  17.  *  References: "Self-Adjusting Binary Search Trees", by Daniel Sleator and
  18.  *              Robert Tarjan.  Journal of the Association for Computing
  19.  *              Machinery Vol 32, No. 3, July 1985 pp. 652-686
  20.  *
  21.  * -------------------------------------------------------------------------- **
  22.  *
  23.  *  This library is free software; you can redistribute it and/or
  24.  *  modify it under the terms of the GNU Library General Public
  25.  *  License as published by the Free Software Foundation; either
  26.  *  version 2 of the License, or (at your option) any later version.
  27.  *
  28.  *  This library is distributed in the hope that it will be useful,
  29.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  30.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  31.  *  Library General Public License for more details.
  32.  *
  33.  *  You should have received a copy of the GNU Library General Public
  34.  *  License along with this library; if not, write to the Free
  35.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36.  *
  37.  * -------------------------------------------------------------------------- **
  38.  *
  39.  * $Log: ubi_SplayTree.h,v $
  40.  * Revision 2.5  1997/07/26 04:15:46  crh
  41.  * + Cleaned up a few minor syntax annoyances that gcc discovered for me.
  42.  * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
  43.  *
  44.  * Revision 2.4  1997/06/03 05:22:56  crh
  45.  * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
  46.  * problems.
  47.  *
  48.  * Revision 2.3  1995/10/03 22:19:37  CRH
  49.  * Ubisized!
  50.  * Also, added the function ubi_sptSplay().
  51.  *
  52.  * Revision 2.1  95/03/09  23:55:04  CRH
  53.  * Added the ModuleID static string and function.  These modules are now
  54.  * self-identifying.
  55.  * 
  56.  * Revision 2.0  95/02/27  22:34:55  CRH
  57.  * This module was updated to match the interface changes made to the
  58.  * ubi_BinTree module.  In particular, the interface to the Locate() function
  59.  * has changed.  See ubi_BinTree for more information on changes and new
  60.  * functions.
  61.  *
  62.  * The revision number was also upped to match ubi_BinTree.
  63.  *
  64.  *
  65.  * Revision 1.0  93/10/15  22:59:36  CRH
  66.  * With this revision, I have added a set of #define's that provide a single,
  67.  * standard API to all existing tree modules.  Until now, each of the three
  68.  * existing modules had a different function and typedef prefix, as follows:
  69.  *
  70.  *       Module        Prefix
  71.  *     ubi_BinTree     ubi_bt
  72.  *     ubi_AVLtree     ubi_avl
  73.  *     ubi_SplayTree   ubi_spt
  74.  *
  75.  * To further complicate matters, only those portions of the base module
  76.  * (ubi_BinTree) that were superceeded in the new module had the new names.
  77.  * For example, if you were using ubi_AVLtree, the AVL node structure was
  78.  * named "ubi_avlNode", but the root structure was still "ubi_btRoot".  Using
  79.  * SplayTree, the locate function was called "ubi_sptLocate", but the next
  80.  * and previous functions remained "ubi_btNext" and "ubi_btPrev".
  81.  *
  82.  * This was not too terrible if you were familiar with the modules and knew
  83.  * exactly which tree model you wanted to use.  If you wanted to be able to
  84.  * change modules (for speed comparisons, etc), things could get messy very
  85.  * quickly.
  86.  *
  87.  * So, I have added a set of defined names that get redefined in any of the
  88.  * descendant modules.  To use this standardized interface in your code,
  89.  * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
  90.  * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or
  91.  * datatype names for the module that you are using.  Just remember to
  92.  * include the header for that module in your program file.  Because these
  93.  * names are handled by the preprocessor, there is no added run-time
  94.  * overhead.
  95.  *
  96.  * Note that the original names do still exist, and can be used if you wish
  97.  * to write code directly to a specific module.  This should probably only be
  98.  * done if you are planning to implement a new descendant type, such as
  99.  * red/black trees.  CRH
  100.  *
  101.  * Revision 0.0  93/04/21  23:07:13  CRH
  102.  * Initial version, written by Christopher R. Hertel.
  103.  * This module implements Splay Trees using the ubi_BinTree module as a basis.
  104.  *
  105.  * ========================================================================== **
  106.  */
  107.  
  108. #include "ubi_BinTree.h" /* Base binary tree functions, types, etc.  */
  109.  
  110. /* ========================================================================== **
  111.  * Function prototypes...
  112.  */
  113.  
  114. ubi_trBool ubi_sptInsert( ubi_btRootPtr  RootPtr,
  115.                           ubi_btNodePtr  NewNode,
  116.                           ubi_btItemPtr  ItemPtr,
  117.                           ubi_btNodePtr *OldNode );
  118.   /* ------------------------------------------------------------------------ **
  119.    * This function uses a non-recursive algorithm to add a new element to the
  120.    * splay tree.
  121.    *
  122.    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
  123.    *                       the root of the tree to which NewNode is to be added.
  124.    *           NewNode  -  a pointer to an ubi_btNode structure that is NOT
  125.    *                       part of any tree.
  126.    *           ItemPtr  -  A pointer to the sort key that is stored within
  127.    *                       *NewNode.  ItemPtr MUST point to information stored
  128.    *                       in *NewNode or an EXACT DUPLICATE.  The key data
  129.    *                       indicated by ItemPtr is used to place the new node
  130.    *                       into the tree.
  131.    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
  132.    *                       the tree, a duplicate node may be found.  If
  133.    *                       duplicates are allowed, then the new node will
  134.    *                       be simply placed into the tree.  If duplicates
  135.    *                       are not allowed, however, then one of two things
  136.    *                       may happen.
  137.    *                       1) if overwritting *is not* allowed, this
  138.    *                          function will return FALSE (indicating that
  139.    *                          the new node could not be inserted), and
  140.    *                          *OldNode will point to the duplicate that is
  141.    *                          still in the tree.
  142.    *                       2) if overwritting *is* allowed, then this
  143.    *                          function will swap **OldNode for *NewNode.
  144.    *                          In this case, *OldNode will point to the node
  145.    *                          that was removed (thus allowing you to free
  146.    *                          the node).
  147.    *                          **  If you are using overwrite mode, ALWAYS  **
  148.    *                          ** check the return value of this parameter! **
  149.    *                 Note: You may pass NULL in this parameter, the
  150.    *                       function knows how to cope.  If you do this,
  151.    *                       however, there will be no way to return a
  152.    *                       pointer to an old (ie. replaced) node (which is
  153.    *                       a problem if you are using overwrite mode).
  154.    *
  155.    *  Output:  a boolean value indicating success or failure.  The function
  156.    *           will return FALSE if the node could not be added to the tree.
  157.    *           Such failure will only occur if duplicates are not allowed,
  158.    *           nodes cannot be overwritten, AND a duplicate key was found
  159.    *           within the tree.
  160.    * ------------------------------------------------------------------------ **
  161.    */
  162.  
  163. ubi_btNodePtr ubi_sptRemove( ubi_btRootPtr RootPtr, ubi_btNodePtr DeadNode );
  164.   /* ------------------------------------------------------------------------ **
  165.    * This function removes the indicated node from the tree.
  166.    *
  167.    *  Input:   RootPtr  -  A pointer to the header of the tree that contains
  168.    *                       the node to be removed.
  169.    *           DeadNode -  A pointer to the node that will be removed.
  170.    *
  171.    *  Output:  This function returns a pointer to the node that was removed
  172.    *           from the tree (ie. the same as DeadNode).
  173.    *
  174.    *  Note:    The node MUST be in the tree indicated by RootPtr.  If not,
  175.    *           strange and evil things will happen to your trees.
  176.    * ------------------------------------------------------------------------ **
  177.    */
  178.  
  179. ubi_btNodePtr ubi_sptLocate( ubi_btRootPtr RootPtr,
  180.                              ubi_btItemPtr FindMe,
  181.                              ubi_trCompOps CompOp );
  182.   /* ------------------------------------------------------------------------ **
  183.    * The purpose of ubi_btLocate() is to find a node or set of nodes given
  184.    * a target value and a "comparison operator".  The Locate() function is
  185.    * more flexible and (in the case of trees that may contain dupicate keys)
  186.    * more precise than the ubi_btFind() function.  The latter is faster,
  187.    * but it only searches for exact matches and, if the tree contains
  188.    * duplicates, Find() may return a pointer to any one of the duplicate-
  189.    * keyed records.
  190.    *
  191.    *  Input:
  192.    *     RootPtr  -  A pointer to the header of the tree to be searched.
  193.    *     FindMe   -  An ubi_btItemPtr that indicates the key for which to
  194.    *                 search.
  195.    *     CompOp   -  One of the following:
  196.    *                    CompOp     Return a pointer to the node with
  197.    *                    ------     ---------------------------------
  198.    *                   ubi_trLT - the last key value that is less
  199.    *                              than FindMe.
  200.    *                   ubi_trLE - the first key matching FindMe, or
  201.    *                              the last key that is less than
  202.    *                              FindMe.
  203.    *                   ubi_trEQ - the first key matching FindMe.
  204.    *                   ubi_trGE - the first key matching FindMe, or the
  205.    *                              first key greater than FindMe.
  206.    *                   ubi_trGT - the first key greater than FindMe.
  207.    *  Output:
  208.    *     A pointer to the node matching the criteria listed above under
  209.    *     CompOp, or NULL if no node matched the criteria.
  210.    *
  211.    *  Notes:
  212.    *     In the case of trees with duplicate keys, Locate() will behave as
  213.    *     follows:
  214.    *
  215.    *     Find:  3                       Find: 3
  216.    *     Keys:  1 2 2 2 3 3 3 3 3 4 4   Keys: 1 1 2 2 2 4 4 5 5 5 6
  217.    *                  ^ ^         ^                   ^ ^
  218.    *                 LT EQ        GT                 LE GE
  219.    *
  220.    *     That is, when returning a pointer to a node with a key that is LESS
  221.    *     THAN the target key (FindMe), Locate() will return a pointer to the
  222.    *     LAST matching node.
  223.    *     When returning a pointer to a node with a key that is GREATER
  224.    *     THAN the target key (FindMe), Locate() will return a pointer to the
  225.    *     FIRST matching node.
  226.    *
  227.    *  See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
  228.    * ------------------------------------------------------------------------ **
  229.    */
  230.  
  231. ubi_btNodePtr ubi_sptFind( ubi_btRootPtr RootPtr,
  232.                            ubi_btItemPtr FindMe );
  233.   /* ------------------------------------------------------------------------ **
  234.    * This function performs a non-recursive search of a tree for any node
  235.    * matching a specific key.
  236.    *
  237.    *  Input:
  238.    *     RootPtr  -  a pointer to the header of the tree to be searched.
  239.    *     FindMe   -  a pointer to the key value for which to search.
  240.    *
  241.    *  Output:
  242.    *     A pointer to a node with a key that matches the key indicated by
  243.    *     FindMe, or NULL if no such node was found.
  244.    *
  245.    *  Note:   In a tree that allows duplicates, the pointer returned *might
  246.    *          not* point to the (sequentially) first occurance of the
  247.    *          desired key.  In such a tree, it may be more useful to use
  248.    *          ubi_sptLocate().
  249.    * ------------------------------------------------------------------------ **
  250.    */
  251.  
  252. void ubi_sptSplay( ubi_btRootPtr RootPtr,
  253.                    ubi_btNodePtr SplayMe );
  254.   /* ------------------------------------------------------------------------ **
  255.    *  This function allows you to splay the tree at a given node, thus moving
  256.    *  the node to the top of the tree.
  257.    *
  258.    *  Input:
  259.    *     RootPtr  -  a pointer to the header of the tree to be splayed.
  260.    *     SplayMe  -  a pointer to a node within the tree.  This will become
  261.    *                 the new root node.
  262.    *  Output: None.
  263.    *
  264.    *  Notes:  This is an uncharacteristic function for this group of modules
  265.    *          in that it provides access to the internal balancing routines,
  266.    *          which would normally be hidden.
  267.    *          Splaying the tree will not damage it (assuming that I've done
  268.    *          *my* job), but there is overhead involved.  I don't recommend
  269.    *          that you use this function unless you understand the underlying
  270.    *          Splay Tree principles involved.
  271.    * ------------------------------------------------------------------------ **
  272.    */
  273.  
  274. int ubi_sptModuleID( int size, char *list[] );
  275.   /* ------------------------------------------------------------------------ **
  276.    * Returns a set of strings that identify the module.
  277.    *
  278.    *  Input:  size  - The number of elements in the array <list>.
  279.    *          list  - An array of pointers of type (char *).  This array
  280.    *                  should, initially, be empty.  This function will fill
  281.    *                  in the array with pointers to strings.
  282.    *  Output: The number of elements of <list> that were used.  If this value
  283.    *          is less than <size>, the values of the remaining elements are
  284.    *          not guaranteed.
  285.    *
  286.    *  Notes:  Please keep in mind that the pointers returned indicate strings
  287.    *          stored in static memory.  Don't free() them, don't write over
  288.    *          them, etc.  Just read them.
  289.    * ------------------------------------------------------------------------ **
  290.    */
  291.  
  292. /* -------------------------------------------------------------------------- **
  293.  * Masquarade...
  294.  *
  295.  * This set of defines allows you to write programs that will use any of the
  296.  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
  297.  * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
  298.  * including the appropriate module header.
  299.  */
  300.  
  301. #undef ubi_trInsert
  302. #undef ubi_trRemove
  303. #undef ubi_trLocate
  304. #undef ubi_trFind
  305. #undef ubi_trModuleID
  306.  
  307. #define ubi_trInsert( Rp, Nn, Ip, On ) \
  308.         ubi_sptInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
  309.                        (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
  310.  
  311. #define ubi_trRemove( Rp, Dn ) \
  312.         ubi_sptRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
  313.  
  314. #define ubi_trLocate( Rp, Ip, Op ) \
  315.         ubi_sptLocate( (ubi_btRootPtr)(Rp), \
  316.                        (ubi_btItemPtr)(Ip), \
  317.                        (ubi_trCompOps)(Op) )
  318.  
  319. #define ubi_trFind( Rp, Ip ) \
  320.         ubi_sptFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
  321.  
  322. #define ubi_trModuleID( s, l ) ubi_sptModuleID( s, l )
  323.  
  324. /* ================================ The End ================================= */
  325. #endif /* ubi_SplayTree_H */
  326.  
  327.  
  328.  
  329.  
  330.  
  331.